package bam;


import java.util.ArrayList;

public class Bank {

       private ArrayList<Account> account;
       private int count;
       private String id;
       private String password;
       private String name;
       private String personId;
       private String email;
       private double balance;

       public Bank () {
       account=new ArrayList<Account>();
       count=0;
      }

    public Account register(int id,String password,String name,String personId,String email,String style){
        if(style.equals("SavingsAccount")){
    	  account.add(new SavingsAccount(id,password,name,personId,email,0));
        }
        else{
        	account.add(new CreditAccount(id, password, name, personId, email, 0,0));
        }

      return account.get(count++);
    }

    public Account Register(int id,String password){
    	for(int i=0;i<account.size();i++){
    		if(id==account.get(i).getId()&&password.equals(account.get(i).getPassword()))
    			return account.get(i);
    	}
    	return null;
    }


    public Account deposit(int id,double x){
     account.get(id-100001).setBalance(account.get(id-100001).getBalance()+x);
     return account.get(id-100001);
    }


    public Account withdraw(int id,double x){
      if(account.get(id-100001).getBalance()>=x)
          account.get(id-100001).setBalance(account.get(id-100001).getBalance()-x);

      return account.get(id-100001);
    }


    public Account setceil(int id,double x){
    	if(new CreditAccount().equals(account.get(id-100001))){
            CreditAccount c=(CreditAccount)account.get(id-100001);
            return c;
        }
          return null;
    }


   public double getSumOfCustomers(){
       double sum=0;
      for(int i=0;i<account.size();i++){
         sum+=account.get(i).getBalance();
      }
       return sum;
   }

   public double getSumOfCustomerCeiling(){
      double sum=0;
      for(int i=0;i<account.size();i++){
          if(new CreditAccount().equals(account.get(i)))
              sum+=account.get(i).getBalance();
      }
       return sum;
   }
   public Account Loan(int id,double x){
       if(new LoanCreditAccount().equals(account.get(id-100001))){
          LoanCreditAccount c = (LoanCreditAccount) account.get(id-100001);
          c.requestLoan(x);
          return c;
       }
       else if(new LoanSavingAccount().equals(account.get(id-100001))){
           LoanSavingAccount c = (LoanSavingAccount) account.get(id-100001);
           c.requestLoan(x);
           return c;
       }
       return null;
   }
   public Account payLoan(int id,double x){
       if(new LoanCreditAccount().equals(account.get(id-100001))){
          LoanCreditAccount c = (LoanCreditAccount) account.get(id-100001);
          c.payLoan(x);
          return c;
       }
       else if(new LoanSavingAccount().equals(account.get(id-100001))){
           LoanSavingAccount c = (LoanSavingAccount) account.get(id-100001);
           c.payLoan(x);
           return c;
       }
       return null;
   }
   public double sumOfLoan(){
       double sum=0;
   
       for(int i=0;i<account.size();i++){
          if(new LoanCreditAccount().equals(account.get(i-100001))){
          LoanCreditAccount c = (LoanCreditAccount) account.get(i-100001);
             sum+=c.getLoan();
       }
       else if(new LoanSavingAccount().equals(account.get(i-100001))){
           LoanSavingAccount c = (LoanSavingAccount) account.get(i-100001);
          sum+=c.getLoan();
       }   
     } 
       return sum;
}
}

